home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / pstopls.zoo / ps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-04  |  4.7 KB  |  186 lines

  1. /*
  2.  
  3.   ps.c: a program to print MiNT process statuses
  4.  
  5.   
  6.   Coded: 3/24/1991 by Tony Reynolds, cctony@sgisci1.ocis.olemiss.edu
  7.   For use with: MiNT release 0.7
  8.   Updated: 3/29/1991 by Eric Smith for MiNT version 0.8
  9.   Updated again 8/9/1991 by ERS for MiNT 0.9.
  10.   Updated some more 11/91 by AKP (shows process time in fractions of a sec).
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <osbind.h>
  15. #include <errno.h>
  16. #include <basepage.h>
  17. #include <ioctl.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21.  
  22. #define ulong unsigned long
  23.  
  24. struct status {   /* defines values for internal->external process states */
  25.   int mint;       /* what MiNT knows */
  26.   char desc[10];  /* a textual description for above */
  27. } proc_stat[] = {
  28.   0,    "Ready",
  29.   0x01, "Ready",
  30.   0x20, "Wait ",
  31.   0x21, "Sleep",
  32.   0x22, "Exit ",
  33.   0x02, "TSR  ",
  34.   0x24, "Stop ",
  35.   -1,    "Unkn "
  36.  };   /* initialized from data in procfs.c */
  37.  
  38. extern int __mint;
  39.  
  40. typedef struct _context {
  41.     long    regs[15];    /* registers d0-d7, a0-a6 */
  42.     long    usp;        /* user stack pointer (a7) */
  43.     short    sr;        /* status register */
  44.     long    pc;        /* program counter */
  45.     long    ssp;        /* supervisor stack pointer */
  46.     long    term_vec;    /* GEMDOS terminate vector (0x102) */
  47.  
  48. /* these fields were added in MiNT 0.9 */
  49.     char    fstate[216];    /* FPU internal state */
  50.     char    fregs[12*8];    /* registers fp0-fp7 */
  51.     long    fctrl[3];        /* FPCR/FPSR/FPIAR */
  52.     short    sfmt;            /* stack frame format identifier */
  53.     long    iar;            /* instruction address */
  54.     short    internal[4];    /* four words of internal info */
  55. } CONTEXT;
  56.  
  57. struct pinfo {
  58.     long     sysstack;
  59.     CONTEXT    ctxts[2];
  60.     long    magic;
  61.     char    *base;
  62.     short    pid, ppid, pgrp;
  63.     short    ruid, rgid;
  64.     short    euid, egid;
  65.     short    memflags;
  66.     short    pri;
  67.     short    wait_q;
  68.     long    wait_cond;
  69.     /* (all times are in milliseconds) */
  70.     ulong    systime;        /* time spent in kernel        */
  71.     ulong    usrtime;        /* time spent out of kernel    */
  72.     ulong    chldstime;        /* children's kernel time     */
  73.     ulong    chldutime;        /* children's user time        */
  74.  
  75.     ulong    maxmem;            /* max. amount of memory to use */
  76.     ulong    maxdata;        /* max. data region for process */
  77.     ulong    maxcore;        /* max. core memory for process */
  78.     ulong    maxcpu;            /* max. cpu time to use     */
  79.  
  80.     short    domain;            /* process domain (TOS or MiNT)    */
  81.     short    curpri;            /* current priority (nice)    */
  82. };
  83.  
  84. /* open the process named "name" */
  85. int
  86. open_proc(name)
  87.   char *name;
  88. {
  89.   static char pname[] = "u:\\proc\\xxxxxxxx.xxx";
  90.  
  91.   strcpy(pname+8, name);
  92.   return open(pname, 0);
  93. }
  94.  
  95. void
  96. main(argc, argv)
  97.   int argc;
  98.   char **argv;
  99. {
  100.  
  101.   _DTA mydta;
  102.   int fd;
  103.   int fserror=0;
  104.   long place;
  105.   short pid, ppid, pri, curpri;
  106.   char *cmdline;
  107.   struct status *statp;
  108.   BASEPAGE bpage;        /* process basepage read in here if possible */
  109.   struct pinfo proc;        /* process info read in here */
  110.   long ptime;
  111.   long hour, min, sec, frac;
  112.  
  113.   Fsetdta(&mydta);          /* give the OS my new DTA */
  114.  
  115.   fserror=Fsfirst("u:\\proc\\*.*", 0x27);
  116.   if (fserror < 0) {
  117.     fprintf(stderr, "ps: cannot list processes, MiNT may not be running\n");
  118.     exit(1);
  119.   }
  120.  
  121.   printf("PID  PPID PRI CURPRI STATUS   SIZE    TIME    COMMAND\n");
  122.  
  123.   /* okay, now run the Fsnext bit through the wringer */
  124.  
  125.   fserror = E_OK;       /* set up for the loop */
  126.  
  127.   while (fserror == E_OK ) {
  128.     fd = open_proc(mydta.dta_name);
  129.  
  130.     for (cmdline = mydta.dta_name; *cmdline && *cmdline != '.'; cmdline++) ;
  131.     *cmdline = 0;
  132.  
  133.     if (fd < 0) goto next;
  134.  
  135.     ioctl(fd, PPROCADDR, &place);
  136.     lseek(fd, place, 0);
  137.     read(fd, &proc, sizeof(proc));
  138.     if ((pid = proc.pid) < 0) pid = 0;
  139.     if ((ppid = proc.ppid) < 0) ppid = 0;
  140.     pri = proc.pri;
  141.     curpri = proc.curpri;
  142.     ptime = proc.systime + proc.usrtime;
  143.     hour = (ptime/1000/60/60);
  144.     min = (ptime/1000/60)%60;
  145.     sec = (ptime/1000)%60;
  146.     frac = (ptime%1000) / 10;    /* (never anything in .001 digit) */
  147.  
  148.     ioctl(fd, PBASEADDR, &place);
  149.     lseek(fd, place, 0);
  150.     read(fd, &bpage, sizeof(bpage));
  151.     cmdline = bpage.p_cmdlin;
  152.     if (*cmdline) *cmdline = ' ';
  153.     close(fd);
  154.  
  155.     statp = proc_stat;      /* hunt down string referring to
  156.                    process status */
  157.     while (statp->mint != mydta.dta_attribute &&
  158.        statp->mint >= 0) statp++;
  159.  
  160. /*        PID  PPID PRI CURPRI STATUS   SIZE     TIME   COMMAND */
  161.     if (hour) {
  162.     printf("%03d  %03d %3d   %3d  %s  %8ld %02ld:%02ld:%02ld  %s%s\n",
  163.         pid, ppid, pri, curpri, statp->desc,
  164.         mydta.dta_size,
  165.         hour,
  166.         min,
  167.         sec,
  168.         mydta.dta_name,
  169.         cmdline);
  170.     }
  171.     else {
  172.     printf("%03d  %03d %3d   %3d  %s  %8ld %02d:%02d.%02d  %s%s\n",
  173.         pid, ppid, pri, curpri, statp->desc,
  174.         mydta.dta_size,
  175.         min,
  176.         sec,
  177.         frac,
  178.         mydta.dta_name,
  179.         cmdline);
  180.     }
  181. next:
  182.     fserror = Fsnext();
  183.   }
  184.   exit(0);
  185. }
  186.